package com.chenxin.wms.controller;

import com.chenxin.common.annotation.Log;
import com.chenxin.common.core.controller.BaseController;
import com.chenxin.common.core.domain.AjaxResult;
import com.chenxin.common.exception.ServiceException;
import com.chenxin.wms.domain.dto.ContainerCallRequestDTO;
import com.chenxin.wms.domain.dto.ContainerRequestDTO;
import com.chenxin.wms.domain.dto.TaskRequestDTO;
import com.chenxin.wms.domain.dto.WipMaterialRegisterParams;
import com.chenxin.wms.process.check.ICheckProcess;
import com.chenxin.wms.process.inbound.impl.ProcWipMaterialRegister;
import com.chenxin.wms.process.stock.IStockProcess;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/pda")
public class PdaController extends BaseController {
    @Autowired
    private ProcWipMaterialRegister wipMaterialRegister;
    @Autowired
    private ICheckProcess checkProcess;
    @Autowired
    private IStockProcess stockProcess;

    @PostMapping("/wipMaterialRegister")
    public AjaxResult wipMaterialRegister(@RequestBody WipMaterialRegisterParams dto) {
        return AjaxResult.success("操作成功");
    }

    @PostMapping("check-affirm")
    @Log(title = "盘点确认")
    public AjaxResult checkAffirm(@RequestBody TaskRequestDTO request) {
        if (null == request.getTaskId()) {
            throw new ServiceException("任务Id不能为空！");
        }
        request.setUserId(this.getUserId());
        try {
            return checkProcess.checkAffirm(request);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("操作失败" + e.getMessage());
        }
    }

    @PostMapping("empty-register")
    @Log(title = "空框入库")
    public AjaxResult emptyRegister(@RequestBody ContainerRequestDTO request) {
        if (StringUtils.isBlank(request.getContNo())) {
            throw new ServiceException("托盘条码不能为空");
        }
        return AjaxResult.success("空托盘登记入库");
    }

    @PostMapping("empty-call")
    @Log(title = "空框呼叫")
    public AjaxResult emptyCall(@RequestBody ContainerCallRequestDTO request) {
        //功能说明：只需要根据站台就可知呼叫哪个库区（托盘库或料箱库）
        if (StringUtils.isBlank(request.getStationNo())) {
            throw new ServiceException("站台编号不能为空");
        }
        if (request.getPlanQty() <= 0) {
            throw new ServiceException("托盘数量不能小于等于0");
        }
        return AjaxResult.success("空托盘呼叫出库");
    }

    @PostMapping("outAffirm")
    @Log(title = "出库领料确认")
    public AjaxResult outAffirm(@RequestBody TaskRequestDTO request) {
        if (null == request.getTaskId()) {
            throw new ServiceException("任务Id不能为空！");
        }
        if (null == request.getContNo()) {
            throw new ServiceException("托盘条码不能为空！");
        }
        if (null == request.getSpuNo()) {
            throw new ServiceException("物料编号不能为空！");
        }
        if (null == request.getLocateNo()) {
            throw new ServiceException("当前位置不能为空！");
        }
        request.setUserId(this.getUserId());
        try {
            return stockProcess.outAffirm(request);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("操作失败" + e.getMessage());
        }
    }
}
